#include "searchandreplace.h"
#include "ui_searchandreplace.h"

SearchAndReplace::SearchAndReplace(QWidget *parent, TextEdit* text, bool replace) : QDialog(parent, Qt::Dialog), ui(new Ui::SearchAndReplace)
{
    ui->setupUi(this);
    (replace) ? setWindowTitle(tr("Remplacer")) : setWindowTitle(tr("Rechercher"));

    ui->tabWidget->setCurrentWidget(ui->rechercher);
    ui->searchStr->setFocus();

    if(replace)
    {
        ui->tabWidget->setCurrentWidget(ui->remplacer);
        ui->searchStr_2->setFocus();
    }

    textEdit = text;

    setFixedSize(444, 218);
}

SearchAndReplace::~SearchAndReplace()
{
    delete ui;
}

void SearchAndReplace::on_btnNext_clicked()
{
    if(!ui->searchStr->text().isEmpty())
        searchText(ui->searchStr->text());
}

QTextDocument::FindFlags SearchAndReplace::getFlags(bool replace)
{
    QTextDocument::FindFlags flags = 0;

    if(!replace)
    {
        if(ui->searchUp->isChecked())
            flags = flags | QTextDocument::FindBackward;
        if(ui->wholeWord->isChecked())
            flags = flags | QTextDocument::FindWholeWords;
        if(ui->caseSensitive->isChecked())
            flags = flags | QTextDocument::FindCaseSensitively;
     }

    else
    {
        if(ui->searchUp_2->isChecked())
            flags = flags | QTextDocument::FindBackward;
        if(ui->wholeWord_2->isChecked())
            flags = flags | QTextDocument::FindWholeWords;
        if(ui->caseSensitive_2->isChecked())
            flags = flags | QTextDocument::FindCaseSensitively;
    }

    return flags;
}

bool SearchAndReplace::searchText(QString searchStr, bool replace, bool notify)
{
    QTextDocument::FindFlags flags = getFlags(replace);

    if(!textEdit->find(searchStr, flags))
    { /* Aucun résultat : On repart du début, au cas où */

        QTextCursor cur = textEdit->textCursor();
        int oldPosition = cur.position();
        cur.setPosition(0);

        if(!textEdit->find(searchStr, flags))
        {
            cur.setPosition(oldPosition);
            textEdit->setTextCursor(cur); /* On remet le curseur où il était */
            if(notify) QMessageBox::information(this, tr("Rechercher..."), tr("Fin du script atteinte. Recherche terminée."), QMessageBox::Ok);
            return false;
        }
    }

    return true;
}

void SearchAndReplace::on_btnNext_2_clicked()
{
    if(!ui->searchStr_2->text().isEmpty())
        searchText(ui->searchStr_2->text(), true);
}

void SearchAndReplace::on_btnReplace_clicked()
{
    if(textEdit->textCursor().hasSelection())
    {
        textEdit->textCursor().clearSelection();
        textEdit->textCursor().movePosition(QTextCursor::NextWord, QTextCursor::KeepAnchor);
        textEdit->textCursor().insertText(ui->replaceStr->text());
        searchText(ui->searchStr_2->text(), true);
    }
}

void SearchAndReplace::on_btnReplaceAll_clicked()
{
    QTextCursor cur = textEdit->textCursor();
    int oldPosition = cur.position();
    cur.movePosition(QTextCursor::Start);
    textEdit->setTextCursor(cur);

    int nbReplaced = 0;

    if(!ui->searchStr_2->text().isEmpty())
    {
        while(textEdit->find(ui->searchStr_2->text()))
        {
            if(textEdit->textCursor().hasSelection())
            {
                textEdit->textCursor().clearSelection();
                textEdit->textCursor().movePosition(QTextCursor::NextWord, QTextCursor::KeepAnchor);
                textEdit->textCursor().insertText(ui->replaceStr->text());
                ++nbReplaced;
            }
        }
    }

    cur.setPosition(oldPosition);
    textEdit->setTextCursor(cur);

    QMessageBox::information(this, tr("Remplacement terminé"), tr("Traitement achevé. ")+QString::number(nbReplaced)+tr(" occurences remplacées."));
}

void SearchAndReplace::on_btnSearchAll_clicked()
{
    QString userInput(ui->searchStr->text());
    QTextDocument::FindFlags flags;

    if(!userInput.isEmpty())
    {
        if(ui->wholeWord->isChecked()) flags = flags | QTextDocument::FindWholeWords;
        if(ui->caseSensitive->isChecked()) flags = flags | QTextDocument::FindCaseSensitively;

        QMap<int, QString> results;

        QTextCursor cursor = textEdit->textCursor();
        int oldPosition = cursor.position();
        cursor.movePosition(QTextCursor::Start); // retour au début du script
        textEdit->setTextCursor(cursor);

        while (textEdit->find(userInput, flags))
        {
            results.insert(textEdit->textCursor().blockNumber()+1, textEdit->textCursor().block().text().trimmed());
        }

        cursor.setPosition(oldPosition);
        textEdit->setTextCursor(cursor);

        emit searchAllResults(results, userInput);
    }
}
